import type { VxeGridProps } from '#/adapter/vxe-table';

import { h } from 'vue';

import { z } from '#/adapter/form';
import { renderDictTag } from '#/utils/render';

/**
 * 性别选项
 */
export const sexOptions = [
  {
    color: 'blue',
    label: '男',
    value: '1',
    dictLabel: '男',
    dictValue: '1',
  },
  {
    color: 'pink',
    label: '女',
    value: '0',
    dictLabel: '女',
    dictValue: '0',
  },
] as any[];

export const columns: VxeGridProps['columns'] = [
  {
    title: '用户姓名',
    field: 'userName',
    width: 120,
  },
  {
    title: '联系电话',
    field: 'phone',
    width: 130,
  },
  {
    title: '年龄',
    field: 'age',
    width: 80,
  },
  {
    title: '性别',
    field: 'sex',
    width: 80,
    slots: {
      default: ({ row }) => {
        return row.sex !== null && row.sex !== undefined
          ? renderDictTag(row.sex, sexOptions)
          : '';
      },
    },
  },
  {
    title: '联系人名称',
    field: 'contactName',
    width: 120,
  },
  {
    title: '联系人号码',
    field: 'contactPhone',
    width: 130,
  },
  {
    title: '描述',
    field: 'medicalRemark',
    minWidth: 200,
  },
  {
    title: '病历文件',
    field: 'medicalFile',
    width: 120,
    slots: {
      default: ({ row }) => {
        return row.medicalFile ? (
          <a
            href={row.medicalFile.filePath}
            rel="noopener noreferrer"
            target="_blank"
          >
            查看文件
          </a>
        ) : (
          '暂无文件'
        );
      },
    },
  },
  {
    title: '创建时间',
    field: 'gmtCreated',
    width: 160,
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    resizable: false,
    width: 'auto',
  },
];

export const modalSchema = (
  handleUpload: (file: File) => void,
  handleRemove: () => void,
) => [
  {
    component: 'Input',
    fieldName: 'userName',
    label: '姓名',
    rules: 'required',
  },
  {
    component: 'Select',
    componentProps: {
      options: sexOptions,
    },
    fieldName: 'sex',
    label: '性别',
  },
  {
    component: 'InputNumber',
    componentProps: {
      min: 1,
      max: 120,
    },
    fieldName: 'age',
    label: '年龄',
  },
  {
    component: 'Input',
    fieldName: 'phone',
    label: '手机号码',
    rules: z
      .string()
      .min(1, { message: '请输入手机号' })
      .regex(/^1[3,4578]\d{9}$/, { message: '请输入正确的手机号' }),
  },
  {
    component: 'Input',
    fieldName: 'contactName',
    label: '联系人名称',
  },
  {
    component: 'Input',
    fieldName: 'contactPhone',
    label: '联系人号码',
    rules: z
      .string()
      .regex(/^1[3,4578]\d{9}$/, { message: '请输入正确的手机号' })
      .optional()
      .or(z.literal('')),
  },
  {
    component: 'Textarea',
    componentProps: {
      rows: 4,
    },
    fieldName: 'medicalRemark',
    label: '描述',
  },
  {
    component: 'Upload',
    componentProps: {
      // 更多属性见：https://ant.design/components/upload-cn
      accept: '.pdf',
      // 自动携带认证信息
      // customRequest: uploadApi,
      disabled: false,
      maxCount: 1,
      multiple: false,
      showUploadList: true,
      // 上传列表的内建样式，支持四种基本样式 text, picture, picture-card 和 picture-circle
      listType: 'text',
      beforeUpload: (file: File) => {
        handleUpload(file);
        return false;
      },
      onRemove: () => {
        handleRemove();
      },
    },
    fieldName: 'medicalFile',
    label: '病历文件',
    renderComponentContent: () => {
      return {
        default: () =>
          h(
            'span',
            {
              class: 'vben-link',
            },
            '上传病历文件',
          ),
      };
    },
  },
];
